//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-4-15
// @Time:上午11:01
//

#ifndef TESTSERVER_IOTDATABASEFACTORY_H
#define TESTSERVER_IOTDATABASEFACTORY_H

#include "SQLiteCpp/Database.h"
#include "Business/internaltypes.h"
#include <iostream>
#include <vector>


class IoTDataBaseFactory
{
private:
    // 数据库 对象,sqliteWrapper 传入
    SQLite::Database *m_Db = nullptr;
public:
    IoTDataBaseFactory(SQLite::Database *mDb);

    int initIoTTable();

    int initSensorDataTable();

    int initDeviceTable();

    int initGatewayTable();

    int initDeviceLoginTable();

    /**
     * @brief 添加/查找 一个网关 \n 如果 存在我的设备 则直接返回 , 否则创建
     * @param [in]uid 用户id
     * @param [out]outGid 超找到的 网关 列表
     * @return 0 -- 成功 ,  <0  --失败
     */
    int addOrFindGatewayId(int64_t uid, std::vector<uint64_t> &outGids);

    /**
     * @brief 添加 一个 设备
     * @param uid 用户id
     * @param GatewayId 网关
     * @param permission 权限
     * @param note 备注
     * @param outDeviceid 新增的 设备id
     * @return 0 -- 成功 ,  <0  --失败
     */
    int addDevice(int64_t uid, uint64_t GatewayId, int32_t permission, std::string &note, uint64_t &outDeviceid);

    int updataDeviceToken(int64_t uid, uint64_t GatewayId, uint64_t DeviceId, std::string &newToken);

    /**
     * @brief 查找设备
     * @param [out]devices
     * @param [in]uid 用户 id , 0 则查询 公开的 设备
     * @param [in]GatewayId
     * @param [in]permission
     * @return 0 -- 成功 ,  <0  --失败
     */
    int findDevices(std::vector<DeviceDataType> &devices, int64_t uid, uint64_t GatewayId, int32_t permission = 4);

    /**
     * @brief
     * @return
     */
    int insertData(SensorDataType &data);

    int findDataByUid(
            std::vector<SensorDataType> &outdatas, int64_t uid, uint64_t GatewayId = 0, uint64_t DevideId = 0,
            int32_t permission = 0, int32_t key = 0, int limit = 0, int direction = 0, int64_t targetTime = 0);

    int checkMyDevice(int64_t uid, uint64_t GatewayId, uint64_t DevideId);

    int findDataByPublic(
            std::vector<SensorDataType> &outdatas, uint64_t GatewayId, uint64_t DevideId, int size, int direction,
            int64_t targetTime);

    int loginDeviceByToken(std::string &token);

    /**
     * @brief 检验是否 登录
     * @param [in]GatewayId
     * @param [in]DevideId
     * @return
     */
    bool checkLoginDevice(uint64_t GatewayId, uint64_t DevideId, int64_t &lastLogid);

    int logoutDevice(uint64_t GatewayId, uint64_t DevideId, int64_t lastLogid);

    int HeartBeatDevice(uint64_t GatewayId, uint64_t DevideId, int64_t lastLogid);

    int findDeviceByToken(DeviceDataType &deviceDataType, std::string token);

    int findDeviceToken(DeviceDataType &deviceDataType, int64_t uid, uint64_t GatewayId, uint64_t DeviceId);
};


#endif //TESTSERVER_IOTDATABASEFACTORY_H
